package com.bilibili.bilibili.utils;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * 项目名称：bilibili
 * 类描述：
 * 创建人：${丁江楠}
 * 创建时间：2017/3/10 18:51
 * 修改人：${丁江楠}
 * 修改时间：2017/3/10 18:51
 * 修改备注：
 */
public class RecyclerViewUtils {

       static LoadingData data;


        public static void OnScrollListener(final Activity activity, RecyclerView recyclerView) {

            data = (LoadingData) activity;

            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

                boolean isSlidingToLast = false;

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                    LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();

                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                        int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();

                        int totalItemCount = manager.getItemCount();

                        if (lastVisibleItem == (totalItemCount - 1) && !isSlidingToLast) {

                            data.onLoadMore();//当滑动到底部的时候触动这个方法


                        }
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    if (dx > 0) {

                        isSlidingToLast = true;

                    } else {

                        isSlidingToLast = false;

                    }

                }
            });
        }

        public interface LoadingData {

            void onLoadMore();
        }

}
